<?php
/**
 * Created by PhpStorm.
 * User: Shine
 * Date: 2019-02-23
 * Time: 15:17
 */

namespace app\m\controller;

use think\Db;

class User
{

    public function login()
    {
        $ses = session('ses');
        $code = $_GET['code'];
        $phone = $_GET['phone'];
        $uid = $ses['uid'];
        $oid = $ses['oid'];
        if (!$ses || !$code || !$phone) {
            echo '{"err":1,"msg":"param error"}';
            return;
        }
        $now = $_SERVER['REQUEST_TIME'];
        $cut = $now - 600;
        $qs = Db::query('select id from x1_sms where oid=? and phone=? and code=? and time>=?', [$oid, $phone, $code, $cut]);
        if (!$qs) {
            echo '{"err":1001,"msg":"验证码错误"}';
            return;
        }
        $qs = Db::query('select id,uid,nickname,avatar from wx_ac where id=?', [$oid]);
        if (!$qs) {
            echo '{"err":1002,"msg":"用户信息不存在"}';
            return;
        }
        $ac = $qs[0];
        if (empty($ac['avatar'])) {
            $ac['avatar'] = 'https://c.g56.cn/x9/user_im.png';
        }
        $qs = Db::query('select uid from user where phone=?', [$phone]);
        //用户是否存在
        if ($qs) {
            if ($uid == $qs[0]['uid']) {
                Db::execute('update user set login_time=? where uid=?', [$now, $uid]);
                echo json_encode(['err' => 0, 'msg' => '登录成功', 'user' => $ac, 'uAuth' => 2]);
            } else {
                $uid = $qs[0]['uid'];
                Db::execute('update wx_ac set uid=? where id=?', [$uid, $oid]);
                Db::execute('update user set oid=?,login_time=? where uid=?', [$oid, $now, $uid]);
                $ac['uid'] = $uid;
                echo json_encode(['err' => 0, 'msg' => '登录成功', 'user' => $ac, 'uAuth' => 2]);
            }
        } else {
            $data = ['oid' => $oid, 'phone' => $phone, 'create_time' => $now, 'login_time' => $now];
            $uid = Db::table('user')->insertGetId($data);
            Db::execute('update wx_ac set uid=? where id=?', [$uid, $oid]);
            $ac['uid'] = $uid;
            echo json_encode(['err' => 0, 'msg' => '登录成功', 'user' => $ac, 'uAuth' => 2]);
        }
        $ses['uid'] = $uid;
        session('ses', $ses);
    }


    /**
     * 微信授权页面
     */
    public function wx_auth()
    {

        $sc = isset($_GET['sc']) ? $_GET['sc'] : '0';

        if ($sc == '1') {
            $note = '需要获取昵称、图像，请点击允许。';

        } else {
            $note = '为了更好的用户体验，我们会获取昵称和图像信息，请同意授权。';
        }

        $a = ['title' => '河南校足办', 'note' => $note, 'btn' => '确定'];


        $rev = ['err' => 0, 'msg' => 'ok', 'a' => $a];

        echo json_encode($rev);

    }

    /*
     * 更新用户微信昵称、图像信息
     */
    public function user_info()
    {
        $rawData = $_GET['rawData'];
        $ses = session('ses');
        // if ($ses && sha1($rawData . $ses['key']) == $_GET['signature']) {
        $u = json_decode($rawData, true);
        $avatar = $u['avatarUrl'];
        // $avatarUrl = substr($avatarUrl, 0, strrpos($avatarUrl, '/'));
        $now = $_SERVER['REQUEST_TIME'];
        Db::execute('update wx_ac set nickname=?,avatar=?,gender=?,city=?,province=?,update_time=? where id=?', [
            $u['nickName'],
            $avatar,
            $u['gender'],
            $u['city'],
            $u['province'],
            //$u['language'],
            $now,
            $ses['oid']
        ]);
        // 尝试更新 user_ico
        // $ico = $avatarUrl . '/132';
        $uid = $ses['uid'];
        if ($uid > 0) {
            // 更新用户图像
            // Db::execute('update x9_user set ico=? where uid=?', [$avatarUrl, $uid]);
        }
        $user = ['id' => $ses['oid'], 'uid' => $ses['uid'], 'nickname' => $u['nickName'], 'avatar' => $avatar, 'uAuth' => 0];

        echo json_encode(['err' => 0, 'msg' => 'ok', 'user' => $user]);

    }

    /**
     * 我的
     */
    public function my()
    {
        $ses = session('ses');
        if (!$ses) {
            echo '{"err":1,"msg":"auth error"}';
            return;
        }
        // $oid = $ses['oid'];
        $uid = $ses['uid'];
        if ($uid < 1) {
            echo '{"err":0,"msg":"no login"}';
            return;
        }
        $qs = Db::query('select name,phone,avatar,login_time from user where uid=?', [$uid]);
        if (!$qs) {
            echo '{"err":1001,"msg":"用户信息不存在"}';
            return;
        }
        $user = $qs[0];

        $login_time = $user['login_time'];

        // 统计信息
        $stat = [];

        // 未读消息
        $qs = Db::query('select count(*) as num from x1_msg where uid=? and read_time=0 ', [$uid]);
        $msn = $qs[0]['num'] ? $qs[0]['num'] : 0;

        // 认证状态
        /*
        $qs = Db::query('select pid,type,result from x1_role_apply where uid=? order by result ', [$uid]);
        $apply = '未申请';
        foreach ($qs as $v) {
            if ($v['result'] == 2) {
                $apply = "已认证";
            }
            if ($apply != '已认证') {
                if ($v['result'] == 0) {
                    $apply = "未审核";
                }
                if ($v['result'] == 3) {
                    $apply = "已拒绝";
                }
                if ($v['result'] == 6) {
                    $apply = "已撤销";
                }
            }
        }
        */

        //是否已认证，上传
        $is_verify = 0;
        $qs = Db::query('select * from x1_user_role where uid=? and state=1', [$uid]);
        if ($qs) {
            $is_verify = 1;
        }

        $stat['verify'] = '未认证';


        //审核
        $is_check = 0;   //是否显示审核
        $check_num = 0;
        $qs =[];//  Db::query('select r.id,r.type,r.pid,n.area,area.pc from x1_user_role r left join x1_school s on r.pid=s.id left join x1_unit n on r.pid=n.id left join area on n.area=area.code  where r.uid=? and r.state=1 order by r.id desc', [$uid]);
        $role = [];
        if ($qs) {
            foreach ($qs as $v) {
                if ($v['type'] <= 2) {
                    $is_check = 1;
                    $role = $v;
                }
            }
        }

        if ($is_check == 1) {
            if ($role['type'] == 1) {
                if ($role['pc'] == 0) {
                    //省级的人员可以审核省级和本省的市级人员
                    $check_num = Db::table('x1_role_apply a')->join('area', 'a.area=area.code', 'LFET')->where('a.type', 1)->where("area.pc ='{$role['area']}' or area.code ='{$role['area']}'")->count();
                } elseif ($role['pc'] % 100 == 0) {
                    //市级人员审核区级
                    $check_num = Db::table('x1_role_apply a')->join('area', 'a.area=area.code', 'LFET')->where('a.type', 1)->where('area.pc', $role['area'])->count();
                } else {
                    //区级人员审核学校
                    $check_num = Db::table('x1_role_apply')->where('type', 2)->where('area', $role['area'])->count();
                }
            } else {
                $check_num = Db::table('x1_role_apply')->where('pid', $role['pid'])->where('type', 3)->where('result', 0)->count();
            }
        }
        
        echo json_encode(['err' => 0, 'msg' => '更新用户信息成功', 'stat' => $stat]);
    }


}
